package com.cowin.sys.controller;

import com.cowin.base.commom.ProjectConstant;
import com.cowin.base.result.Result;
import com.cowin.base.result.ResultGenerator;
import com.cowin.sys.model.vo.*;
import com.cowin.sys.service.TeOfmVehiclePlanService;
import com.cowin.sys.service.TmOfmOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by  on 2019/10/30.
 *
 * @author CodeGenerator
 */
@Slf4j
@Api(value = "TeOfmVehiclePlanController", description = "整车计划信息控制器", tags = "TeOfmVehiclePlanController", consumes = "application/json")
@RestController
@RequestMapping("/te/ofm/vehicle/plan")
public class TeOfmVehiclePlanController {
    @Resource
    private TeOfmVehiclePlanService teOfmVehiclePlanService;
    @Resource
    TmOfmOrderService tmOfmOrderService;

    @ApiOperation(value = "计划报表 明细 汇总")
    @PostMapping("/form")
    public Result getPlanForm(@RequestBody TeOfmVehiclePlanVo teOfmVehiclePlanVo) {
        PageHelper.startPage(teOfmVehiclePlanVo.getPage(), teOfmVehiclePlanVo.getLimit());
        List<Map<String, Object>> list = teOfmVehiclePlanService.getPlanForm(teOfmVehiclePlanVo);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "计划查询 批次总数量 锁定 订单 释放")
    @PostMapping("/search")
    public Result searchPlan(@RequestBody PlanSearchVo planSearchVo) {
        PageHelper.startPage(planSearchVo.getPage(), planSearchVo.getLimit());
        List<PlanSearchVo> list = teOfmVehiclePlanService.searchPlan(planSearchVo);
        PageInfo<PlanSearchVo> pageInfo = new PageInfo<>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "计划查询 批次号 整车物料号 数量 销售类型 加工类型 订单号 计划导入时间 订单生成时间 释放时间")
    @PostMapping("/search/order")
    public Result searchPlanByOrder(@RequestBody SearchPlanOrderVo searchPlanOrderVo) {
        if (searchPlanOrderVo.getPage() != -1 && searchPlanOrderVo.getLimit() != -1) {
            //导出全部
            PageHelper.startPage(searchPlanOrderVo.getPage(), searchPlanOrderVo.getLimit());
            List<SearchPlanOrderVo> list = teOfmVehiclePlanService.searchPlanByOrder(searchPlanOrderVo);
            PageInfo<SearchPlanOrderVo> pageInfo = new PageInfo<>(list);
            return ResultGenerator.genSuccessResult(pageInfo);
        } else {
            List<SearchPlanOrderVo> list = teOfmVehiclePlanService.searchPlanByOrder(searchPlanOrderVo);
            return ResultGenerator.genSuccessResult(list);
        }
    }

    @ApiOperation(value = "批次交付率 锁定 焊装20 涂装20 总装30")
    @PostMapping("/delivery")
    public Result getDelivery(@RequestBody PlanSearchVo planSearchVo) {
        String batchNo = planSearchVo.getBatchNo();
        int num = planSearchVo.getNum();

        List<PlanDeliveryVo> list = teOfmVehiclePlanService.getDelivery(batchNo, num);
        Map<String, BatachDlivery> map = new HashMap<>();
        list.forEach(e -> {
            String batchNoStr = e.getBatchNo();
            String codeValue = e.getCodeValue();
            int count = e.getCount();
            BatachDlivery value = map.get(batchNoStr);
            if (value == null) {
                BatachDlivery batachDlivery = new BatachDlivery(0, 0, 0, 0, 0);
                batachDlivery.setBatchNo(batchNoStr);
                batachDlivery.setPlanCount(e.getTotal());
                if (ProjectConstant.SD11_20.equals(codeValue)) {
                    batachDlivery.setLockCount(count);
                } else if (ProjectConstant.HZ11_20.equals(codeValue)) {
                    batachDlivery.setWelding(count);
                } else if (ProjectConstant.TZ11_20.equals(codeValue)) {
                    batachDlivery.setPainting(count);
                } else if (ProjectConstant.ZZ11_30.equals(codeValue)) {
                    batachDlivery.setAssembly(count);
                } else if (ProjectConstant.ZZ11_20.equals(codeValue)) {
                    batachDlivery.setAssemblyTwenty(count);
                }
                map.put(batchNoStr, batachDlivery);
            } else {
                value.setPlanCount(e.getTotal());
                value.setBatchNo(batchNoStr);
                if (ProjectConstant.SD11_20.equals(codeValue)) {
                    value.setLockCount(count);
                } else if (ProjectConstant.HZ11_20.equals(codeValue)) {
                    value.setWelding(count);
                } else if (ProjectConstant.ZZ11_30.equals(codeValue)) {
                    value.setAssembly(count);
                } else if (ProjectConstant.ZZ11_20.equals(codeValue)) {
                    value.setAssemblyTwenty(count);
                } else if (ProjectConstant.TZ11_20.equals(codeValue)) {
                    value.setPainting(count);
                }
            }
        });
        List<BatachDlivery> batachDliveries = new ArrayList<>();
        map.forEach((k, v) -> batachDliveries.add(v));
        Map<String, DeliveryCount> deliveryCountByBatchNo = tmOfmOrderService.getDeliveryCountByBatchNo(map);
        batachDliveries.forEach(e -> {
            mergeData(e, deliveryCountByBatchNo);
        });
        return ResultGenerator.genSuccessResult(batachDliveries);
    }

    /**
     * 功能描述:合并数据判断是否过10点显示Echars图表 <br>
     * <>
     *
     * @param:
     * @return:
     * @author:yuchao
     * @date: 2020/4/10 10:51
     */
    private BatachDlivery mergeData(BatachDlivery batachDlivery,  Map<String, DeliveryCount> deliveryCountByBatchNo) {
        int lockCount = batachDlivery.getLockCount();
        int welding = batachDlivery.getWelding();
        int painting = batachDlivery.getPainting();
        int assemblyTwenty = batachDlivery.getAssemblyTwenty();
        int assembly = batachDlivery.getAssembly();
        if (lockCount > 0) {
            batachDlivery.setLockIsShow(Boolean.TRUE);
        } else if (lockCount == 0 && welding == 0 && painting == 0 && assemblyTwenty == 0 && assembly == 0) {
            batachDlivery.setLockIsShow(deliveryCountByBatchNo.get(batachDlivery.getBatchNo()).getUnLockCount() > 0);
        } else {
            batachDlivery.setLockIsShow(Boolean.FALSE);
            batachDlivery.setLockCount(batachDlivery.getPlanCount());
        }
        if (welding > 0) {
            batachDlivery.setWeldingIsShow(Boolean.TRUE);
        } else if (welding == 0 && painting == 0 && assemblyTwenty == 0 && assembly == 0) {
            batachDlivery.setWeldingIsShow(deliveryCountByBatchNo.get(batachDlivery.getBatchNo()).getHz10Count() > 0);
        } else {
            batachDlivery.setWeldingIsShow(Boolean.FALSE);
            batachDlivery.setWelding(batachDlivery.getPlanCount());
        }
        if (painting > 0) {
            batachDlivery.setPaintingIsShow(Boolean.TRUE);
        } else if (painting == 0 && assemblyTwenty == 0 && assembly == 0) {
            batachDlivery.setPaintingIsShow(deliveryCountByBatchNo.get(batachDlivery.getBatchNo()).getTz10Count() > 0);
        } else {
            batachDlivery.setPaintingIsShow(Boolean.FALSE);
            batachDlivery.setPainting(batachDlivery.getPlanCount());
        }
        if (assemblyTwenty > 0) {
            batachDlivery.setAssemblyTwentyIsShow(Boolean.TRUE);
        } else if (assemblyTwenty == 0 && assembly == 0) {
            batachDlivery.setAssemblyTwentyIsShow(deliveryCountByBatchNo.get(batachDlivery.getBatchNo()).getZz10Count() > 0);
        } else {
            batachDlivery.setAssemblyTwentyIsShow(Boolean.FALSE);
            batachDlivery.setAssemblyTwenty(batachDlivery.getPlanCount());
        }
        if (assembly > 0) {
            batachDlivery.setAssemblyIsShow(Boolean.TRUE);
        } else if (assembly == 0) {
            batachDlivery.setAssemblyIsShow(deliveryCountByBatchNo.get(batachDlivery.getBatchNo()).getZz20Count() > 0);
        } else {
            batachDlivery.setAssemblyIsShow(Boolean.FALSE);
        }
        return batachDlivery;
    }


}
